package com.hjy.scheduled.solr.Controller;

import com.hjy.scheduled.mysql.entity.quartz;
import com.hjy.scheduled.solr.SolrRepository.QuartzSolrRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SolrController {

    @Autowired
    private QuartzSolrRepository quartzRepository;

    /**
     * 新增到solr
     * @param name
     * @param type
     * @param cron
     * @return
     */
    @PostMapping(value = "/solrsave")
    @ResponseBody
    public quartz save(String name,short type,String cron){
        quartz q = new quartz();
        q.setName(name);
        q.setType(type);
        q.setCron(cron);
        return quartzRepository.save(q);
    }

    /**
     * 根据name删除
     * @param name
     */
    @PostMapping(value = "/solrDeleteByName")
    @ResponseBody
    public void solrDeleteByName(String name){
        List<quartz> q = quartzRepository.findByName(name);
    //    quartzRepository.delete(q);

        for (quartz qq : q) {
            quartzRepository.delete(qq);
        }
    }

    /**
     * 新增到solr
     * @param name
     * @return
     */
    @PostMapping(value = "/findbyname")
    @ResponseBody
    public List<quartz> findbyname(String name){
        return quartzRepository.findByName(name);
    }

    /**
     * 新增到solr
     * @param name
     * @return
     */
    @PostMapping(value = "/findbynameall")
    @ResponseBody
    public List<quartz> findbynameAll(String name){
        return quartzRepository.findAll(name,null);
    }
}
